package cn.itcast.web.controller.cargo;

import cn.itcast.service.cargo.ContractService;
import cn.itcast.vo.ContractProductVo;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import utils.DownloadUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/contract/")
public class ExcelController extends BaseController {

 @Reference
 private ContractService contractService;

 /**
  * 进入下载页面
  * /cargo/contract/print.do
  */
 @RequestMapping("/print")
 public String print() {
  return "cargo/print/contract-print";
 }

 /**
  * 打印出货表
  * /cargo/contract/printExcel.do?inputDate=2015-09
  */
 @RequestMapping("/printExcel")
 public void printExsel(String inputDate) throws IOException {
  //根据船期查询数据
  List<ContractProductVo> list = contractService.findByShipTime(inputDate + "%");
  //构造Excel文件
  //创建工作薄
  Workbook wb = new XSSFWorkbook();
  //创建第一页
  Sheet sheet = wb.createSheet("Sheet1");
  //构造大标题
  //创建第一行
  Row row = sheet.createRow(0);
  //创建第二个单元格
  Cell cell = row.createCell(1);
  //设置单元格内容
  inputDate = inputDate.replaceAll("-0", "-").replaceAll("-", "年");
  cell.setCellValue(inputDate+"月份出货表");
  //构造小标题
  //创建第二行
  row = sheet.createRow(1);
  String[] titles = {"", "客户", "订单号", "货号", "数量", "工厂", "工厂交期", "船期", "贸易条款"};
  for (int i = 0; i < titles.length; i++) {
   String title = titles[i];
   //创建每一个单元格
   cell = row.createCell(i);
   //设置单元格内容
   cell.setCellValue(title);
  }
  //循环数据列表，构造数据行内容
  int index = 2;
  for (ContractProductVo cp : list) {
   row = sheet.createRow(index);

   cell = row.createCell(1);
   cell.setCellValue(cp.getCustomName());

   cell = row.createCell(2);
   cell.setCellValue(cp.getContractNo());

   cell = row.createCell(3);
   cell.setCellValue(cp.getProductNo());

   cell = row.createCell(4);
   cell.setCellValue(cp.getCnumber());

   cell = row.createCell(5);
   cell.setCellValue(cp.getFactoryName());

   cell = row.createCell(6);
   cell.setCellValue(cp.getDeliveryPeriod());

   cell = row.createCell(7);
   cell.setCellValue(cp.getShipTime());

   cell = row.createCell(8);
   cell.setCellValue(cp.getTradeTerms());

   index ++;
  }
  //文件下载
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  wb.write(bos);
  new DownloadUtil().download(bos,response,"出货表.xlsx");
 }

 /**
  * 使用EasyExcel完成Excel的生成和下载
  * /cargo/contract/printEasyExcel.do?inputDate=2015-01
  */
 @RequestMapping("/printEasyExcel")
 public void printEasyExcel(String inputDate) throws IOException {
  //查询数据
  List<ContractProductVo> list = contractService.findByShipTime(inputDate + "%");
  //设置下载信息
  response.setContentType("application/vnd.ms-excel"); //下载excel
  response.setCharacterEncoding("utf-8");
  String fileName = URLEncoder.encode("出货表", "UTF-8");
  response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  //调用EasyExcel的工具类完成生成下载
  EasyExcel.write(response.getOutputStream())
   .head(ContractProductVo.class)
   .sheet("heima31")
   .doWrite(list);
 }

 /**
  * 使用模板下载
  * /cargo/contract/printTemplate.do?inputDate=2015-01
  */
 @RequestMapping("/printTemplate")
 public void printTemplate(String inputDate) throws IOException {
  //准备数据
  List<ContractProductVo> list = contractService.findByShipTime(inputDate + "%");
  Map map = new HashMap();
  inputDate = inputDate.replaceAll("-0", "-").replaceAll("-", "年");
  map.put("time", inputDate);
  map.put("title1", "客户名称");
  //设置下载信息
  response.setContentType("application/vnd.ms-excel"); //下载excel
  response.setCharacterEncoding("utf-8");
  String fileName = URLEncoder.encode("出货表", "UTF-8");
  response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  //加载Excel模板
  String path = session.getServletContext().getRealPath("/")+ "/make/tOUTPRODUCT.xlsx";
  //创建EasyExcel的excelWtire对象
  ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
   .head(ContractProductVo.class)
   .withTemplate(path)
   .build();
  //获取sheet对象
  WriteSheet sheet = EasyExcel.writerSheet().build();
  //调用方法完成填充map数据
  excelWriter.fill(map, sheet);
  //调用方法完成填充list数据
  excelWriter.fill(list, sheet);
  //属性资源，完成下载
  excelWriter.finish();
 }
}
